﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using DevComponents.DotNetBar;
using QLTuyenXe.BLL;
using QLTuyenXe.DAL;
using QLTuyenXe.DAO;
using QLTuyenXe.GUI;
namespace QLTuyenXe
{
    public partial class frmMain : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public static string username ;
        THU thu = new THU();
        BUser user = new BUser();
        public frmMain()
        {
            InitializeComponent();
        }
        public frmMain(string a)
            : this()
        {
            username = a;

        }
        //gan cho doi mat khau
        

        //Kiem tra tab
        private bool checkTab(string name)
        {
            for (int i = 0; i < tbCtHienThi.Tabs.Count; i++)
            {
                if (tbCtHienThi.Tabs[i].Text == name)
                {
                    tbCtHienThi.SelectedTabIndex = i;
                    return true;
                }
            }
            return false;
        }

        private void btnTaoHD_Click(object sender, EventArgs e)
        {
            if (checkTab("Tạo Hợp Đồng") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Tạo Hợp Đồng");
                frmHopDong frm01 = new frmHopDong();
                frm01.Dock = DockStyle.Fill;
                frm01.FormBorderStyle = FormBorderStyle.None;
                frm01.TopLevel = false;
                tab.AttachedControl.Controls.Add(frm01);
                frm01.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void buttonItem14_Click(object sender, EventArgs e)
        {
            if (checkTab("Tuyến xe") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Tuyến xe");
                frmTuyenXe frm08 = new frmTuyenXe();
                //frmTuyenXe.d
                frm08.Dock = DockStyle.Fill;
                frm08.FormBorderStyle = FormBorderStyle.None;
                frm08.TopLevel = false;
                tab.AttachedControl.Controls.Add(frm08);
                frm08.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void btnPhanQuyen_Click(object sender, EventArgs e)
        {
            if (checkTab("Phân Quyền") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Phân Quyền");
                frmPhanQuyen frm05 = new frmPhanQuyen();
                frmPhanQuyen.quyen = username;
                frm05.Dock = DockStyle.Fill;
                frm05.FormBorderStyle = FormBorderStyle.None;
                frm05.TopLevel = false;
                tab.AttachedControl.Controls.Add(frm05);
                frm05.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void btnCongTy_Click(object sender, EventArgs e)
        {
            frmCongTy frm06 = new frmCongTy();
            frmCongTy.ten = username;
            frm06.ShowDialog();
        }


        private void btnSaoLuu_Click(object sender, EventArgs e)
        {
            frmBackUp backup = new frmBackUp();
            backup.ShowDialog();
            
        }

        /*
         private void btnTX_Click(object sender, EventArgs e)
         {
             if (checkTab("Tuyến xe") == false)
             {
                 TabItem tab = tbCtHienThi.CreateTab("Tuyến xe");
                 frmTuyenXe tuyenxe = new frmTuyenXe();
                 tuyenxe.Dock = DockStyle.Fill;
                 tuyenxe.FormBorderStyle = FormBorderStyle.None;
                 tuyenxe.TopLevel = false;
                 tab.AttachedControl.Controls.Add(tuyenxe);
                 tuyenxe.Show();
                 tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
             }
         }*/

        private void btnPhucHoi_Click(object sender, EventArgs e)
        {
            
            Hide();
            frmRestore restore = new frmRestore();
            restore.ShowDialog();
            //this.Close();
            //System.Windows.Forms.Application.Exit();
        }

        private void tbCtHienThi_Click(object sender, EventArgs e)
        {

        }

        private void btnDieuHanh_Click_1(object sender, EventArgs e)
        {
            if (checkTab("Điều hành") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Điều hành");
                frmDieuHanh dieuhanh = new frmDieuHanh();
                dieuhanh.Dock = DockStyle.Fill;
                dieuhanh.FormBorderStyle = FormBorderStyle.None;
                dieuhanh.TopLevel = false;
                tab.AttachedControl.Controls.Add(dieuhanh);
                dieuhanh.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void btnQuanLy_Click(object sender, EventArgs e)
        {
            if (checkTab("Quản lý xe") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Quản lý xe");
                frmQuanLyXe qlx = new frmQuanLyXe();
                qlx.Dock = DockStyle.Fill;
                qlx.FormBorderStyle = FormBorderStyle.None;
                qlx.TopLevel = false;
                tab.AttachedControl.Controls.Add(qlx);
                qlx.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void ribbonPanel1_Click(object sender, EventArgs e)
        {

        }

        private void btnLaiXe_Click(object sender, EventArgs e)
        {
            if (checkTab("Lái xe") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Lái xe");
                frmLaiXe frmLaiXe = new frmLaiXe();
                frmLaiXe.Dock = DockStyle.Fill;
                frmLaiXe.FormBorderStyle = FormBorderStyle.None;
                frmLaiXe.TopLevel = false;
                tab.AttachedControl.Controls.Add(frmLaiXe);
                frmLaiXe.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void btnDoanhThuTuyen_Click(object sender, EventArgs e)
        {
            if (checkTab("Doanh Thu") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Doanh Thu");
                frmDoanhThu frm07 = new frmDoanhThu();
                frmDoanhThu.ten = username;
                frm07.Dock = DockStyle.Fill;
                frm07.FormBorderStyle = FormBorderStyle.None;
                frm07.TopLevel = false;
                tab.AttachedControl.Controls.Add(frm07);
                frm07.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void btnXeThue_Click(object sender, EventArgs e)
        {
            if (checkTab("Giá thuê") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Giá thuê");
                frmGiaThueXe frm04 = new frmGiaThueXe();
                frmGiaThueXe.ten = username;
                frm04.Dock = DockStyle.Fill;
                frm04.FormBorderStyle = FormBorderStyle.None;
                frm04.TopLevel = false;
                tab.AttachedControl.Controls.Add(frm04);
                frm04.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void btnDoanhThuXeThue_Click(object sender, EventArgs e)
        {
            if (checkTab("Danh Sách Hợp Đồng") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Danh Sách Hợp Đồng");
                frmDSHopDong frm02 = new frmDSHopDong();
                frmDSHopDong.ten = username;
                frm02.Dock = DockStyle.Fill;
                frm02.FormBorderStyle = FormBorderStyle.None;
                frm02.TopLevel = false;
                tab.AttachedControl.Controls.Add(frm02);
                frm02.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(username))
            {
                KiemTraQuyen();
            }    
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {

        }

        private void btnDoiMatKhau_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau doiMK = new frmDoiMatKhau();
            frmDoiMatKhau.username1 = username;
            //doiMK.Sender(username);
            doiMK.ShowDialog();
        }

        private void btnDangXuat_Click(object sender, EventArgs e)
        {
            frmDangNhap dangnhap = new frmDangNhap();
            this.Close();
            frmMain m = new frmMain();
            dangnhap.ShowDialog();
            //m.Hide();
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {
        }
        public void KiemTraQuyen()
        {
            DataTable phanQuyen = user.Select_Quyen("name='" + username + "'", "phanquyen= 'X'");
            DataTable doiMK = user.Select_Quyen("name='" + username + "'", "doimatkhau= 'X'");
            DataTable congTy = user.Select_Quyen("name='" + username + "'", "congty= 'X'");
            DataTable saoLuu = user.Select_Quyen("name='" + username + "'", "saoluu= 'X'");
            DataTable phucHoi = user.Select_Quyen("name='" + username + "'", "phuchoi= 'X'");
            DataTable dieuHanh = user.Select_Quyen("name='" + username + "'", "dieuhanh= 'X'");
            DataTable qlyXe = user.Select_Quyen("name='" + username + "'", "quanlyxe= 'X'");
            DataTable laiXe = user.Select_Quyen("name='" + username + "'", "laixe= 'X'");
            DataTable giaTX = user.Select_Quyen("name='" + username + "'", "tuyenxe= 'X'");
            DataTable giaXeThue = user.Select_Quyen("name='" + username + "'", "xethue= 'X'");
            DataTable dtTuyen = user.Select_Quyen("name='" + username + "'", "doanhthu_xechaytuyen= 'X'");
            DataTable dtXeThue = user.Select_Quyen("name='" + username + "'", "doanhthu_xethue= 'X'");
            DataTable hopDong = user.Select_Quyen("name='" + username + "'", "taohopdong= 'X'");
            DataTable baoCao = user.Select_Quyen("name='" + username + "'", "xuatbaocao= 'X'");
            DataTable themKH = user.Select_Quyen("name='" + username + "'", "themKH= 'X'");
            DataTable danhSachKH = user.Select_Quyen("name='" + username + "'", "danhsachKH= 'X'");
            if (phanQuyen.Rows.Count > 0)
            {
                btnPhanQuyen.Enabled = true;
            }
            else
            {
                btnPhanQuyen.Enabled = false;
            }
            if (doiMK.Rows.Count > 0)
            {
                btnDoiMatKhau.Enabled = true;
            }
            else
            {
                btnDoiMatKhau.Enabled = false;
            }
            if (congTy.Rows.Count >0)
            { 
                btnCongTy.Enabled=true;
            }
            else
            {btnCongTy.Enabled =false; }
            if (saoLuu.Rows.Count >0)
            { 
                btnSaoLuu.Enabled = true;
            }
            else{ btnSaoLuu.Enabled =false;}
            if (phucHoi.Rows.Count >0)
            { 
                btnPhucHoi.Enabled=true;
            }
            else{btnPhucHoi.Enabled =false;}
            if (dieuHanh.Rows.Count > 0)
            {
                btnDieuHanh.Enabled = true;
            }
            else
            {
                btnDieuHanh.Enabled = false;
            }

            if (qlyXe.Rows.Count > 0)
            {
                btnQuanLy.Enabled = true;
            }
            else
            {
                btnQuanLy.Enabled = false;
            }

            if (laiXe.Rows.Count > 0)
            {
                btnLaiXe.Enabled = true;
            }
            else
            {
                btnLaiXe.Enabled = false;
            }
            if (giaTX.Rows.Count > 0)
            {
                btnTX.Enabled = true;
            }
            else
            {
                btnTX.Enabled = false;
            }
            if (giaXeThue.Rows.Count > 0)
            {
                btnXeThue.Enabled = true;
            }
            else
            {
                btnXeThue.Enabled = false;
            }
            if (dtTuyen.Rows.Count > 0)
            {
                btnDoanhThuTuyen.Enabled = true;
            }
            else
            {
                btnDoanhThuTuyen.Enabled = false;
            }
            if (dtXeThue.Rows.Count > 0)
            {
                btnDoanhThuXeThue.Enabled = true;
            }
            else
            {
                btnDoanhThuXeThue.Enabled = false;
            }
            if (hopDong.Rows.Count > 0)
            {
                btnTaoHD.Enabled = true;
            }
            else
            {
                btnTaoHD.Enabled = false;
            }
            if (baoCao.Rows.Count > 0)
            {
                btnBaoCaoTuyenXe.Enabled = true;
            }
            else
            {
                btnBaoCaoTuyenXe.Enabled = false;
            }
            if (themKH.Rows.Count > 0)
            {
                btnThemKH.Enabled = true;
            }
            else
            {
                btnThemKH.Enabled = false;
            }
            if (danhSachKH.Rows.Count > 0)
            {
                btnDSKH.Enabled = true;
            }
            else
            {
                btnDSKH.Enabled = false;
            }
        }

        private void btnThemKH_Click(object sender, EventArgs e)
        {
            frmThemKH frmTKH = new frmThemKH();
            frmThemKH.ten = username;
            frmTKH.ShowDialog();
        }

        private void btnDSKH_Click(object sender, EventArgs e)
        {
            if (checkTab("Danh Sách Khách Hàng") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Danh Sách Khách Hàng");
                frmDanhSachKH frmKH = new frmDanhSachKH();
                frmDanhSachKH.ten = username;
                frmKH.Dock = DockStyle.Fill;
                frmKH.FormBorderStyle = FormBorderStyle.None;
                frmKH.TopLevel = false;
                tab.AttachedControl.Controls.Add(frmKH);
                frmKH.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

        private void btnBaoCaoTuyenXe_Click(object sender, EventArgs e)
        {
            if (checkTab("Tuyến xe") == false)
            {
                TabItem tab = tbCtHienThi.CreateTab("Tuyến xe");
                frmTuyenXe frm08 = new frmTuyenXe();
                //frmTuyenXe.d
                frm08.Dock = DockStyle.Fill;
                frm08.FormBorderStyle = FormBorderStyle.None;
                frm08.TopLevel = false;
                tab.AttachedControl.Controls.Add(frm08);
                frm08.Show();
                tbCtHienThi.SelectedTabIndex = tbCtHienThi.Tabs.Count - 1;
            }
        }

       
    }
}
